home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13101 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.6 KB

  1. Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!usenet
  2. From: grantp@usa.pipeline.com(Pete Grant)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Inheritance problem...and other stuff
  5. Date: 23 Mar 1996 14:49:22 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4j131i$qnu@news1.h1.usa.pipeline.com>
  8. References: <4j0ekj$9f6@cloner3.netcom.com>
  9. NNTP-Posting-Host: 38.8.60.7
  10. X-PipeUser: grantp
  11. X-PipeHub: usa.pipeline.com
  12. X-PipeGCOS: (Pete Grant)
  13. X-Newsreader: Pipeline v3.5.0
  14.  
  15. On Mar 23, 1996 09:01:07 in article <Inheritance problem...and other
  16. stuff>, 'pcgpe@ix.netcom.com(Mark Feldman)' wrote: 
  17.  
  18.  
  19. >Heya, 
  20. >I remember seeing a C++ FAQ once but I haven't had any luck finding it 
  21. >again. If someone could point me to it I'd be grateful. 
  22. ftp from rtfm.mit.edu/pub/usenet-by-group/comp.lang.c++/* 
  23. or you can search for old articles here as Marshall Cline 
  24. posts the FAQ here once a month. 
  25.  
  26. >In the mean time I'll go ahead and ask my questions...at the risk of 
  27. >asking something in the FAQ and getting flamed for it :)  
  28. >Here's a generic form of my problem. (BTW I use BC 3.1 and MSVC 4.0 and 
  29. >solutions for either compiler would be fine.) Let's say I have a tree 
  30. >of objects like this: 
  31. [.. code deleted ...] 
  32. >What I want to do however is determine info about the object which is 
  33. >inherited from it's parents. Thus if my object is of type apple then 
  34. >calling apple->isA("apple") should return true. apple->isA("fruit") 
  35. >should also return true, but apple->isA("vegetable") should return 
  36. >false. [.. more deleted ..] 
  37.  
  38. I believe you can come up with a workable solution by utilizing 
  39. RTTI.  AFAI remember, BC3.1 did not have RTTI implemented, but MSVC++4.0 
  40. definitely does.  Read up on typeid and related operators under the 
  41. general topic of RTTI in your on-line documentation. 
  42.  
  43. >2nd Question: 
  44. >What's the standard way of storing such an array to a file and 
  45. >retrieving it? I could always assign an ID number to each class type 
  46. >and load them using case statements but this seems messy. I notice that 
  47. >MFC and OWL seem to use tables to handle the dispatching of windows 
  48. >messages. Would it be feasable to use this for saving the array info to 
  49. >a file? Idealy I would like each function to inherit a serialization 
  50. >function which they can overload to save and restore their data, but 
  51. >how can I make the loading routine know what type of class to create in 
  52. >the first place while still keeping the code tight and clean? 
  53. There's only one way:  store the type information in the file.  An 
  54. enum is probably best; alternately, a name string would work also. 
  55. When you read the data back in, you must check the type info and 
  56. create an object of appropriate type. 
  57.  
  58. There are standard idioms (Coplien's book, for example) for simulating 
  59. virtual constructors -- I have mixed feelings of those since they 
  60. are just a thin veil for hiding what you are really doing.  There's 
  61. really no such thing as a virtual constructor in C++ and I feel a bit 
  62. uneasy with idioms that try to make it look like they exist.  But 
  63. at the same time, I can see the benefits also. 
  64.  
  65. >Am I going about all this the wrong way? For some time now I've 
  66. >considered myself a "good" C++ programmer. When I hit problems like 
  67. >this I start to wonder if I'm really nothing more than a C programmer 
  68. >with a C++ compiler. Feels like playing Zork on a Pentium. 
  69. >All help/suggestions/flames etc appreciated. 
  70. Well, suggestions mostly: 
  71. Read, read, and when you finish, read some more. 
  72. Here's excellent texts: 
  73.  
  74. Advanced C++ (I forget the exact title) by James Coplien 
  75. Effective C++ by Scott Myers 
  76. More Effective C++ by Scott Myers 
  77.  
  78. -- 
  79. Pete Grant 
  80. Kalevi, Inc. 
  81. Software Engineering & development
  82.